/*
 *  Copyright 2020-2025 the original author or authors.
 *  You cannot use this file unless authorized by the author.
 */

package org.ipig.commons.helper;

import java.math.BigDecimal;

/**
 * 请在此处添加注释
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id$$
 * @since 1.0
 */
public class MathHelper {
    /**
     * 等比数列求和
     * <li>公式为Sn=a1(1-q^n)/(1-q) =(a1-an×q)/(1-q) (q≠1)（说明：Sn是前n项和，a1是首项，n是项数，q是公比，an为数列第n项）
     *
     * @param n  long 项数
     * @param a1 BigDecimal 首项
     * @param q  BigDecimal 公比
     * @return BigDecimal
     */
    public static BigDecimal geometricSeriesSum(final long n, final BigDecimal a1, final BigDecimal q) {
        BigDecimal an = a1;
        BigDecimal sn = new BigDecimal(0);
        for (int i = 1; i <= n; i++) {
            sn = sn.add(an);
            an = an.multiply(q);
        }
        return sn;
    }
}

